package com.si.controller.manage;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.si.entity.OperationLog;
import com.si.service.OperationLogService;
import com.si.util.IpAddressUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.si.entity.Blogroll;
import com.si.service.BlogrollService;
/**
 * 友情链接管理
 * @author 10605
 *
 */
@Controller
@RequestMapping("blogroll/manage")
public class BlogrollManageController {
	@Autowired
	private OperationLogService operationLogService;
	@Autowired
	private BlogrollService blogrollService;

	@RequestMapping(value="initBlogroll")
	public String initBlogroll(ModelMap map,HttpServletRequest request){
		List<Blogroll> allBlogroll = blogrollService.getAll();
		map.addAttribute("allBlogroll",allBlogroll);
		//加载友情链接
		List<Blogroll> blogrollList=blogrollService.getAll();
		request.getSession().setAttribute("blogrollList", blogrollList);
		return "managePage/blogroll/initBlogroll";
	}
	
	
	@RequestMapping(value="deleteBlogroll")
	public String deleteBlogroll(@RequestParam(value ="id" ,required = true) Integer id,
															 HttpServletRequest request,	RedirectAttributes redirectAttributes){
		if(id != null){

			Blogroll objectById = (Blogroll) blogrollService.getObjectById(id);
			OperationLog operationLog = new OperationLog();
			operationLog.setMiaoshu("执行成功！");
			operationLog.setOperationdetails("删除友情连接-\""+objectById.getCnBlogrollName()+"\"");
			operationLog.setOperationtime(new Date().toString());
			operationLog.setSensitive("友情连接");
			String ip = IpAddressUtil.getIpAddress(request);
			operationLog.setIpadderse(ip);
			String sysUser = (String) request.getSession().getAttribute("session_sysUser_name");
			operationLog.setUsername(sysUser);
			operationLogService.save(operationLog);
			blogrollService.delete(objectById);

			redirectAttributes.addFlashAttribute("message", "删除成功");
		}else{
			redirectAttributes.addFlashAttribute("message", "删除失败");
		}
		return "redirect:/blogroll/manage/initBlogroll.html";
	} 
	
	
	
	@RequestMapping(value="initSaveBlogroll")
	public String initSaveBlogroll(){
		return "managePage/blogroll/saveBlogroll";
	}
	
	
	@RequestMapping(value="insertBlogroll")
	public String insertBlogroll(HttpServletRequest request,RedirectAttributes RedirectAttributes,Blogroll blogroll){
		if(blogroll != null){
			blogrollService.saveOrUpdate(blogroll);
			RedirectAttributes.addFlashAttribute("message", "添加成功");

			OperationLog operationLog = new OperationLog();
			operationLog.setMiaoshu("执行成功！");
			operationLog.setOperationdetails("添加友情连接-\""+blogroll.getCnBlogrollName()+"\"");
			operationLog.setOperationtime(new Date().toString());
			operationLog.setSensitive("添加友情连接");
			String ip = IpAddressUtil.getIpAddress(request);
			operationLog.setIpadderse(ip);
			String sysUser = (String) request.getSession().getAttribute("session_sysUser_name");
			operationLog.setUsername(sysUser);
			operationLogService.save(operationLog);
		}else{
			RedirectAttributes.addFlashAttribute("message", "添加失败");
		}
		return "redirect:/blogroll/manage/initBlogroll.html";
		
	}
	
	@RequestMapping(value="selectBlogroll")
	public String selectBlogroll(@RequestParam(value="id", required = true)Integer id ,Model model){
		if(id != null ){
			Blogroll blogroll = (Blogroll) blogrollService.getObjectById(id);
			model.addAttribute("blogroll", blogroll);
		}	
		return "managePage/blogroll/findBlogroll";
	}
	
	@RequestMapping(value="updateBlogroll")
	public String updateBlogroll(@RequestParam(value="id", required = true)Integer id ,Model model){
		if(id != null ){
			Blogroll blogroll = (Blogroll) blogrollService.getObjectById(id);
			model.addAttribute("blogroll", blogroll);
		}	
		return "managePage/blogroll/updateBlogroll";
	}
	
	
	@RequestMapping(value="updateDataBlogroll")
	public String updateDataBlogroll(HttpServletRequest request,RedirectAttributes RedirectAttributes,Blogroll blogroll){
		if(blogroll != null ){
			blogrollService.update(blogroll);

			OperationLog operationLog = new OperationLog();
			operationLog.setMiaoshu("执行成功！");
			operationLog.setOperationdetails("修改友情连接-\""+blogroll.getCnBlogrollName()+"\"");
			operationLog.setOperationtime(new Date().toString());
			operationLog.setSensitive("修改友情连接");
			String ip = IpAddressUtil.getIpAddress(request);
			operationLog.setIpadderse(ip);
			String sysUser = (String) request.getSession().getAttribute("session_sysUser_name");
			operationLog.setUsername(sysUser);
			operationLogService.save(operationLog);
			RedirectAttributes.addFlashAttribute("message", "修改成功");
		}	
		return "redirect:/blogroll/manage/initBlogroll.html";
	}
}
